from __future__ import division
from operator import itemgetter, attrgetter
import os
import sys
import math

# step1: load the damn probability thing
# program purpose
# input: the probability for a given query term which we appear in the next 10K queries(so called))
# output: the probability for a given query term which we appear in the next coming query term position
inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/probabilityDistributionEstimationByProf/fourSetOfQueriesByProf20130410/smallBucketsEquallizationMethodByProf20130411/equationsForJuanToSolve"
inputFileHandler = open(inputFileName,"r")
cellKey = ""
probabilityForTheCell = 0.0

cellKeyWithCellProbabilityDict = {}
cellKeyWithOppositeCellProbabilityDict = {}
# The exact probability of what we want
cellKeyWithTargetProbability = {}

for line in inputFileHandler.readlines():
    if line.strip() != "":
        if not line.strip().startswith("Solve"):
            cellKey = line.strip()
            # outputLine = "*" + cellKey + "*"
            # print outputLine
        else:
            probabilityForTheCell = float( line.strip().split("==")[1].split(",")[0].strip() )
            # outputLine = "*" + str(probabilityForTheCell) + "*"
            # print outputLine
            
            if cellKey not in cellKeyWithCellProbabilityDict:
                cellKeyWithCellProbabilityDict[cellKey] = probabilityForTheCell
                cellKeyWithOppositeCellProbabilityDict[cellKey] = 1 - probabilityForTheCell
    else:
        pass

print "len(cellKeyWithCellProbabilityDict):",len(cellKeyWithCellProbabilityDict)
print "cellKeyWithCellProbabilityDict:",cellKeyWithCellProbabilityDict
print "cellKeyWithOppositeCellProbabilityDict:",cellKeyWithOppositeCellProbabilityDict
inputFileHandler.close()


# injection code:
# injection code begins...
del cellKeyWithCellProbabilityDict
del cellKeyWithOppositeCellProbabilityDict
# man-made justification number
cellKeyWithCellProbabilityDict = {}
cellKeyWithOppositeCellProbabilityDict = {}
'''
cellKeyWithCellProbabilityDict["ROW1_0"] = 1.15262492427e-05
cellKeyWithCellProbabilityDict["ROW2_0"] = 0.000660982699686
cellKeyWithCellProbabilityDict["ROW3_0"] = 0.004744877
cellKeyWithCellProbabilityDict["ROW4_0"] = 0.013339567
cellKeyWithCellProbabilityDict["ROW5_0"] = 0.032566185
'''
cellKeyWithCellProbabilityDict["SUM_0"] = 0.000057172

'''
cellKeyWithOppositeCellProbabilityDict["ROW1_0"] = 1 - cellKeyWithCellProbabilityDict["ROW1_0"]
cellKeyWithOppositeCellProbabilityDict["ROW2_0"] = 1 - cellKeyWithCellProbabilityDict["ROW2_0"]
cellKeyWithOppositeCellProbabilityDict["ROW3_0"] = 1 - cellKeyWithCellProbabilityDict["ROW3_0"]
cellKeyWithOppositeCellProbabilityDict["ROW4_0"] = 1 - cellKeyWithCellProbabilityDict["ROW4_0"]
cellKeyWithOppositeCellProbabilityDict["ROW5_0"] = 1 - cellKeyWithCellProbabilityDict["ROW5_0"]
'''
cellKeyWithOppositeCellProbabilityDict["SUM_0"] = 1 - cellKeyWithCellProbabilityDict["SUM_0"]
# injection code ends.


# step2: compute the damn probability(what we need to compute the KL divergence) from another damn probability(what we currently have)
# Solve[(1 - z)^41221 == Pr(sth), z]
# set the init value to z = 0.5

POWER_NUMBER = 41221

for cellKey in cellKeyWithOppositeCellProbabilityDict:
    # for each round, init some variable values:
    z = 0.5
    trueProbability = cellKeyWithOppositeCellProbabilityDict[cellKey]
    previousValueOfZ = 0.0
    previousPredictivedValue = 0.0
    currentPredictivedValue = math.pow((1-z),POWER_NUMBER)
    STEP_PARAMETER_for_LOOP1 = 2
    
    # The most beautiful bound with the STEP_PARAMETER_for_LOOP2 set to be 1000000
    # Option1:
    STEP_PARAMETER_for_LOOP2 = 10000000
    # Option2:
    # NOT very precise, basically for formatting things
    # STEP_PARAMETER_for_LOOP2 = 1000
    
    print "cellKey:",cellKey
    print "*****round1*****"
    while currentPredictivedValue < trueProbability:
        # backup the old things
        previousValueOfZ = z
        previousPredictivedValue = currentPredictivedValue
        
        # update the new things
        z = z - z / STEP_PARAMETER_for_LOOP1
        currentPredictivedValue = math.pow((1-z),POWER_NUMBER)
        # print z,previousPredictivedValue,currentPredictivedValue,trueProbability
    
    print "bounds produced for the round1:"
    print previousValueOfZ,previousPredictivedValue,trueProbability
    print z,currentPredictivedValue,trueProbability
    
    print "*****round2*****"
    # let's try the effects of make things more precise
    while previousPredictivedValue < trueProbability:
        # backup the old things
        previousValueOfZ2 = previousValueOfZ
        previousPredictivedValue2 = previousPredictivedValue
        
        # update the new things
        previousValueOfZ = previousValueOfZ - previousValueOfZ / STEP_PARAMETER_for_LOOP2
        previousPredictivedValue = math.pow((1 - previousValueOfZ),POWER_NUMBER)
        # print previousValueOfZ,previousPredictivedValue2,previousPredictivedValue,trueProbability
    
    print "bounds produced for the round2:"
    print previousValueOfZ2,previousPredictivedValue2,trueProbability
    print previousValueOfZ,previousPredictivedValue,trueProbability
    # I can choose the above first line to record or the above second line to record and it's all my choice
    if cellKey not in cellKeyWithTargetProbability:
        cellKeyWithTargetProbability[cellKey] = previousValueOfZ
    print "*****ending*****"
    print 

print "len(cellKeyWithTargetProbability):",len(cellKeyWithTargetProbability)
print "cellKeyWithTargetProbability:",cellKeyWithTargetProbability

# output components
# for production, comment the following lines out
'''
classLabelList = ["ROW1","ROW2","ROW3","ROW4","ROW5","SUM"]
outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/probabilityDistributionEstimationByProf/fourSetOfQueriesByProf20130410/smallBucketsEquallizationMethodByProf20130411/probabilityWeWantFinally20130418"
outputFileHandler = open(outputFileName,"w")
for classLabel in classLabelList:
    for i in range(0,20):
        cellKey = classLabel + "_" + str(i)
        outputFileHandler.write(cellKey + " " + str(cellKeyWithTargetProbability[cellKey]) + "\n")
outputFileHandler.close()
'''
